#region Usings
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System;
using RapidSpider.Views;
using System.Windows.Forms;
using System.Security.Permissions;
#endregion

namespace RapidSpider.AccountManagementAgents
{
    public static class RapidshareComAccountManagementAgent
    {
        #region Constants
        public const string RAPIDSHARE_PREMIUM_CREATE_ACCOUNT_URI = "http://rapidshare.com/premium.html";
        public const string RAPIDSHARE_PREMIUM_LOGIN_URI = "https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi";
        public const string RAPIDSHARE_PREMIUM_EXTEND_ACCOUNT_URI = "https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi?extend=1";
        #endregion

        #region Properties
        public static Cookie PremiumCookie
        {
            get
            {
                Cookie theCookie = new Cookie(
                    "user",
                    String.Format(
                        CultureInfo.CurrentCulture,
                        "{0}-{1}",
                        RegistryAgent.RapidshareComLogin,
                        RegistryAgent.EscapedRapidsharePassword),
                    "/",
                    ".rapidshare.com");

                theCookie.Expires = DateTime.Parse(
                    "13 November 2024 15:00:00",
                    CultureInfo.CurrentCulture);

                theCookie.HttpOnly = false;

                return theCookie;
            }
        }
        #endregion

        #region Methods
        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public static void CreateAccount(
            IWin32Window theCallingView)
        {
            WebBrowserView theWebBrowserView = new WebBrowserView();

            theWebBrowserView.WebBrowser.Url = new Uri(
                RAPIDSHARE_PREMIUM_CREATE_ACCOUNT_URI);

            theWebBrowserView.ShowDialog(theCallingView);
        }
        
        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public static void ExtendAccount(
            IWin32Window theCallingView)
        {
            HttpWebRequest theRequest = (HttpWebRequest)WebRequest.Create(
                new Uri(RAPIDSHARE_PREMIUM_EXTEND_ACCOUNT_URI));
            theRequest.CookieContainer = new CookieContainer();
            theRequest.CookieContainer.Add(RapidshareComAccountManagementAgent.PremiumCookie);

            using (HttpWebResponse theResponse = (HttpWebResponse)theRequest.GetResponse())
            {
                using (Stream theResponseStream = theResponse.GetResponseStream())
                {
                    using (StreamReader theStreamReader = new StreamReader(theResponseStream))
                    {
                        WebBrowserView theWebBrowserView = new WebBrowserView();

                        theWebBrowserView.WebBrowser.DocumentText =
                            theStreamReader.ReadToEnd()
                                .Replace("href=\"/", "href=\"http://rapidshare.com/")
                                .Replace("src=\"/", "src=\"http://rapidshare.com/");

                        theWebBrowserView.ShowDialog(theCallingView);

                    }
                }
            }
        }
        #endregion
    }
}
